<?php

namespace catchAdmin\settlement\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\settlement\model\Expenses as ExpensesModel;
use catcher\CatchAuth;
use think\facade\Db;
use think\Response;

class Expenses extends CatchController
{
    
    protected $expensesModel;
    
    /**
     *
     * @time 2022/03/10 11:50
     * @param ExpensesModel $expensesModel
     * @return mixed
     */
    public function __construct(ExpensesModel $expensesModel)
    {
        $this->expensesModel = $expensesModel;
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->expensesModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/28 14:25
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $params = $request->filterEmptyField()->post();
        $user = $auth->user();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->expensesModel->storeBy($params),"新建成功");
    }

    /**
     *
     * @time 2022/02/28 14:25
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->expensesModel->updateBy($params['id'], $params),"修改成功");
    }

    /**
     * 删除
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->expensesModel->deleteBy($id),'删除成功');
    }
    /**
     * 打印
     * @time 2022/02/28 15:16
     * @param Request $request
     * @return \think\response\Json
     */
    public function print(Request $request)
    {
        $id = $request->post('id');
        $where = [];
        $where['a.id'] = $id;
        $data = DB::name('db_expenses')->alias('a')
                ->field('a.*,b.entrust_date,b.volume,b.inside_type,b.main_doc_no,b.sub_order_No,b.entrust_company')
                ->join('db_entrust_order b','a.job_number=b.job_number')
                ->where($where)
                ->find();
        $data['created_at'] = date('Y-m-d H:m:s',$data['created_at']);
        $user = DB::name('users')->where('id',$data['creator_id'])->find();
        $data['creator'] = $user['username'];
        $data['merchant_name'] = $user['merchant_name'];
        //$data['entrust_company_info'] = DB::name('db_provider')->where('id',$data['entrust_company'])->find();
        $data['inside_type_name'] = DB::name('db_inside_type')->where('id',$data['inside_type'])->value('name');
        return CatchResponse::success($data, '查询成功');
    }
}